<?php

namespace common\model\sell66;

use common\db\Sell66;

class UserSteam extends Sell66 {
	// 设置当前模型对应的完整数据表名称
	protected $table = 'user_steam';
	public static function createSteamUser($user_id, $steamUserInfo) {
		try {
			$userSteam = new self ();
			$userSteam->user_id = $user_id;
			$userSteam->steam_id = $steamUserInfo ['steam_steamid'];
			$userSteam->communityvisibilitystate = $steamUserInfo ['steam_communityvisibilitystate'];
			$userSteam->personaname = $steamUserInfo ['steam_personaname'];
//			$userSteam->location = $steamUserInfo ['steam_location'];
//			$userSteam->summary = $steamUserInfo ['steam_summary'];
//			$userSteam->lastlogoff = $steamUserInfo ['steam_lastlogoff'];
			$userSteam->avatar_icon = $steamUserInfo ['steam_avatarIcon'];
			$userSteam->avatarmedium = $steamUserInfo ['steam_avatarmedium'];
			$userSteam->avatarfull = $steamUserInfo ['steam_avatarfull'];
//			$userSteam->personastate = $steamUserInfo ['steam_personastate'];
			$userSteam->realname 	= $steamUserInfo ['steam_realname'];
			if (! $userSteam->save ()) {
				return false;
			}
			return $userSteam;
		} catch (\Exception $e) {
			var_dump($e->getMessage());die;
			return false;
		}
	}

    public static function updateUserBySteam($user_id, $steamUserInfo) {
        try {
            $userSteam = self::get($user_id);
            $userSteam->user_id = $user_id;
            $userSteam->steam_id = $steamUserInfo ['steam_steamid'];
            $userSteam->communityvisibilitystate = $steamUserInfo ['steam_communityvisibilitystate'];
            $userSteam->personaname = $steamUserInfo ['steam_personaname'];
//			$userSteam->location = $steamUserInfo ['steam_location'];
//			$userSteam->summary = $steamUserInfo ['steam_summary'];
//			$userSteam->lastlogoff = $steamUserInfo ['steam_lastlogoff'];
            $userSteam->avatar_icon = $steamUserInfo ['steam_avatarIcon'];
            $userSteam->avatarmedium = $steamUserInfo ['steam_avatarmedium'];
            $userSteam->avatarfull = $steamUserInfo ['steam_avatarfull'];
//			$userSteam->personastate = $steamUserInfo ['steam_personastate'];
            $userSteam->realname 	= $steamUserInfo ['steam_realname'];
            $userSteam->save ();
            return $userSteam;
        } catch (\Exception $e) {
            var_dump($e->getMessage());die;
            return false;
        }
    }

	public static function getTokenKey($userId, $steamId) {
		return md5 ( $userId . '|' . $steamId );
	}
	public static function findBySteamId($steamId) {
		return static::get ( [ 
				'steam_id' => $steamId 
		] );
	}
	public static function getInfoByUserIdAndSteamId($userId, $steamId) {
		return static::get ( [ 
				'user_id' => $userId,
				'steam_id' => $steamId 
		] );
	}
	public static function setTradeUrl($userId, $steamId, $tradeUrl,$trade_token) {
		static::where ( [ 
				'user_id' => $userId,
				'steam_id' => $steamId 
		] )->update ( [ 
				'trade_off_url' => $tradeUrl,
				'steam_trade_token'=>$trade_token 
		] );
		return true;
	}
	public function user() {
		return $this->belongsTo ( 'User', 'user_id', 'user_id' );
	}

	public static function getUserInfo($userId)
	{
		$where = array(
			'user_id' => $userId
		);
		return static::get($where);
	}

	/**
	 * 获取用户重复数据处理
	 * @return array
	 */
	public static function getUserRepeatInfo()
	{
		return static::field('count(*) as count, steam_id')->group('steam_id')->having('count > 1')->select();
	}

	/**
	 * 获取重复用户ID
	 * @param  string $steamId 
	 * @return array
	 */
	public static function getUserRepeatBySteamId($steamId)
	{
		$where = array(
			'steam_id' => $steamId
		);

		return static::field('user_id')->where($where)->select();
	}

    /**
     * 删除用户信息
     * @param  int $userId 用户ID
     * @return bool
     */
	public static function delUserSteamByUserId($userId)
	{
		$where = array(
			'user_id' => $userId,
		);

		return static::where($where)->delete();
	}

	public static function getInfoByUserId($userId)
	{
		$where = array(
			'user_id' => $userId,
		);

		$result = static::field('user_id, steam_id, trade_off_url')->where($where)->find();

		return !empty($result) ? $result->toArray() : array();
	}

	public static function getInfoBySteamId($steamId)
	{
		$where = array(
			'steam_id' => $steamId,
		);

		$result = static::field('user_id, steam_id, trade_off_url, game_count, friend_count, player_level')->where($where)->find();

		return !empty($result) ? $result->toArray() : array();
	}

	public static function updateUserBySteamId($steamId, $playerLevel, $gameCount, $friendCount)
	{
		$where = array(
			'steam_id' => $steamId,
		);

		$field = array(
			'player_level' => $playerLevel,
			'game_count' => $gameCount,
			'friend_count' => $friendCount,
		);

		return static::where($where)->update($field);
	}
}